package com.my.mall.client.shoppingCart.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.my.mall.client.shoppingCart.service.ShoppingCartClientService;
import com.my.mall.client.shoppingCart.vo.ShoppingCartClientVo;
import com.my.mall.common.Base;

@Controller
@RequestMapping("client/shoppingCart")
public class ShoppingCartClientController extends Base {

	@Autowired
	ShoppingCartClientService shoppingCartClientService;

	/**
	 * 查询购物车详情
	 * 
	 * @param userId  用户ID
	 * @return
	 */
	@RequestMapping("selectShoppingCartList")
	@ResponseBody
	public Map<String, Object> selectShoppingCartList(int userId) {
		List<ShoppingCartClientVo> list = shoppingCartClientService.selectShoppingCartList(userId);
		Double shoppingCartTotalPrice = shoppingCartClientService.getShoppingCartTotalPrice(list);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("shoppingCartTotalPrice", shoppingCartTotalPrice);
		return map;
	}

	/**
	 * 添加商品到购物车
	 * 
	 * @param id      购物车ID
	 * @param userId  用户ID
	 * @param goodsId 商品ID
	 * @param number  添加数量
	 * @return
	 */
	@RequestMapping("plusGoods")
	@ResponseBody
	public Map<String, Object> plusGoods(int id, int userId, int goodsId, int number) {
		int result = 0;
		if (id == 0)
			result = shoppingCartClientService.save(userId, goodsId, number);
		else
			result = shoppingCartClientService.updatePlus(id, number);
		Map<String, Object> map = selectShoppingCartList(userId);
		if (result == 1) {
			map.put("code", 0);
		} else {
			map.put("code", 500);
			map.put("msg", "系统故障！");
		}
		return map;
	}

	/**
	 * 从购物车中减去一个商品
	 * @param id      购物车ID
	 * @param userId  客户ID
	 * @param number  减去数量
	 * @return
	 */
	@RequestMapping("minusGoods")
	@ResponseBody
	public Map<String, Object> minusGoods(int id, int userId, int number) {
		int result = 0;
		result = shoppingCartClientService.updateMinus(id, number);
		Map<String, Object> map = selectShoppingCartList(userId);
		if (result == 1) {
			map.put("code", 0);
		} else {
			map.put("code", 500);
			map.put("msg", "系统故障！");
		}
		return map;
	}
	
	/**
	 * 从购物车中删除一种商品
	 * @param id
	 * @param userId
	 * @return
	 */
	@RequestMapping("deleteGoods")
	@ResponseBody
	public Map<String, Object> deleteGoods(int id, int userId) {
		int result = shoppingCartClientService.deleteGoods(id);
		Map<String, Object> map = selectShoppingCartList(userId);
		if (result == 1) {
			map.put("code", 0);
		} else {
			map.put("code", 500);
			map.put("msg", "系统故障！");
		}
		return map;
	}
	
	/**
	 * 清空购物车里的所有商品
	 * @param userId
	 * @return
	 */
	@RequestMapping("emptyShoppingCart")
	@ResponseBody
	public Map<String, Object> emptyShoppingCart(int userId) {
		shoppingCartClientService.emptyShoppingCart(userId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("msg", "暂无商品");
		map.put("shoppingCartTotalPrice", 0);
		return map;
	}

}